<?php
class Node extends Controller {
	public function html() {
		$mUser = new User_model();
		$currUser = $mUser->currUser();
		if(!$currUser) header('Location:/auth/login?backurl='.urlencode('/rbac/role/'));
		
		$roleId = $this->get('roleId', 'int');
		if(!$roleId) header('Location:/rbac/role/');
		
		$data = array();
		$tpl = 'rbac/role/node.html';
		
		$mNode = new Node_model();
		$mRole = new Role_model();
		$mRbac = new Rbac_model();
		
		$data['role'] = $mRole->roleById($roleId);
		
		//获取节点信息
		$data['nodeInfo'] = $mNode->nodeAll();
		$data['groupInfo'] = $mNode->groupInfo();
		$data['roleId'] = $roleId;
		
		//获取用户角色节点
		$data['nodes'] = $mRbac->getNodeByRoleId($roleId);
		
		$this->display($data, $tpl);
	}
	public function json() {
		$roleId = $this->post('roleId', 'int');
		$nodeId = $this->post('nodeId', 'arr');
		
		if(!$roleId) $this->displayJSON(array('status' => -1, 'message' => '未知角色ID'));
		
		$mRbac = new Rbac_model();
		$result = $mRbac->role2node($roleId, $nodeId);
		if($result) $this->displayJSON(array('status' => 0, 'message' => '角色节点分配成功'));
		else $this->displayJSON(array('status' => -2, 'message' => '角色节点分配失败'));
	}
}